//
//  ViewController.m
//  CodeaTestFrame
//
//  Created by Innovation_Mbp on 14/12/28.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "ViewController.h"
#import "StandaloneCodeaViewController.h"

#import "GameCenterAddon.h"

@interface ViewController ()
{
    UITableView *codeaTabel;
    NSArray *luaProjectsPath;
    NSArray *commandCodeList;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    luaProjectsPath = [[NSBundle mainBundle] pathsForResourcesOfType:@"" inDirectory:@"Codea"];;
    
    codeaTabel = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    codeaTabel.delegate = self;
    codeaTabel.dataSource = self;
    [self.view addSubview:codeaTabel];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - tableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [luaProjectsPath count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
//    NSString *cellName = [[[luaProjectsPath objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension];
//    cell.textLabel.text = cellName;
    cell.textLabel.text = [[luaProjectsPath objectAtIndex:[indexPath row]] lastPathComponent];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.viewController = [[StandaloneCodeaViewController alloc] initWithProjectAtPath:[luaProjectsPath objectAtIndex:[indexPath row]]];
    [self presentViewController:self.viewController animated:YES completion:NO];

    //Uncomment the following line if you'd like to use the basic GameCenter addon:
//    [self.viewController registerAddon:[GameCenterAddon sharedInstance]];
    //See GameCenterAddon.h for the Lua API description.
}
@end
